<!-- Teacher_StudyCourseQADetail.html -->
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
		<title>学习</title>
		<link rel="stylesheet" type="text/css" href="../../css/mui.min.css">
		<link rel="stylesheet" type="text/css" href="../../css/base.css"/>
		<link rel="stylesheet" href="../../css/CFNetPP_Base.css">
		<style type="text/css" media="screen">
			html,body{
				/*background-color: white;*/
			}
			.touxiang{
	            width: 38px;
	            height: 38px;
	            border-radius: 19px;
	        }
	        .nameLabel{
	            margin-top: 10px;
	            margin-left: 8px;
	            position: absolute;
	            font-size: 14px;
	            color: #222;
	        }
	        .contentDiv{
	            margin-left: 46px;
	            font-size: 14px;
	            text-overflow: -o-ellipsis-lastline;
	            overflow: hidden;
	            text-overflow: ellipsis;
	            display: -webkit-box;
	            /*-webkit-line-clamp: 3;*/
	            -webkit-box-orient: vertical;
	            color: #222;
	            word-wrap:break-word;
	        }
	        .timeSpan{
	            /*display: block;*/
	            margin-top: 5px;
	            margin-left: 46px;
	            color: #666;
	            font-size: 12px;
	        }
	        .question_detail{
	        	background-color: white;
	        	padding: 10px;
	        	/*border-bottom: 1px solid #ddd;*/
	        }
	        /*.answer-div{
	        	background-color: white;
	        }*/
			.mui-table-view-cell{
				padding-left: 24px;
			}
		</style>
	</head>
	<body>
		<div class="question_detail">
			<img id="user_icon" class="touxiang" src="../../icon/icon_default_HeadPortrait.png" alt="">
			<label id="sub_que_user" class="nameLabel">提问人</label>
			<div id="que_detail" class="contentDiv">提问内容</div>
			<span id="sub_que_time" class="timeSpan">提问时间</span>
			<span id="delete_question" class="timeSpan rightSpan" hidden="hidden" onclick="onDeleteQuestionClick()">删除</span>
		</div>
		<ul id="questionList" class="mui-table-view">
			 <!-- <li class="mui-table-view-cell">
			 	<div class="answer-div">
					<img id="user_icon" class="touxiang" src="../../image/default_user_icon.png" alt="">
	    			<label id="sub_que_user" class="nameLabel">提问人</label>
	    			<div id="que_detail" class="contentDiv">提问内容</div>
	    			<span id="sub_que_time" class="timeSpan">提问时间</span>
				</div>
			 </li> -->
		</ul>
		<div class="noSign"  id="noData"  hidden="hidden">
			<!-- <img src="../../image/icon_img.png" width="60%"> -->
			<p class="font16 top_15 base_dark_grey" >
				该问题暂无回复
			</p>
		</div>
		<div class="loadFailure" hidden="hidden" id="noNet">
			<!-- <img src="../../image/icon_img01.png" width="60%"> -->
			<p class="" >
				数据加载失败
			</p>
			<p>
				请检查您的网络
			</p>
			<button type="button" class="mui-btn mui-btn-outlined" onclick="loadNewData()">
				重新加载
			</button>
		</div>
	</body>
	<script type="text/javascript" src="../../script/jquery.min.js"></script>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/MD5.js"></script>
	<script type="text/javascript" src="../../script/requestTool.js"></script>
	<script type="text/javascript" src="../../script/timeStampTool.js"></script>
	<script src="../../script/common.js"></script>
	<script type="text/javascript">
		var page = 1;
		var rows = 10;
		var refreshCount = 0;
		var questionDatas = new Array();
		var currentJid = '0';
		apiready = function(){
			prepareData();
		    setupQA();
		    loadNewData();
		    setRefreshHeader(); 
			setPullUpRefresh();
			setupNotifi();
		}
		//准备数据
		function prepareData(){
			currentJid = $api.getStorage('cfnetppjxid');
			if (api.pageParam.opentype == 'courseCenter') {//如果是课程中心课程
				currentJid = '0';
			}else if(api.pageParam.isxueba == 1) {//如果是学霸赛课程
				currentJid = '419';
			}
		}
		function setupNotifi(){
			api.addEventListener({
			    name: 'sureRemoveNotifi'
			}, function(ret, err) {
			    var data = ret.value;
			    if (data.deletetype == 'questionDetail') {
			    	deleteQuestion(data);
			    }else if(data.deletetype == 'reply'){
			    	deleteReply(data);
			    }
			});
			api.addEventListener({
			    name: 'RefreshRepalyQuestionNotifi'
			}, function(ret, err) {
			    loadNewData();
			});
		}
		function userNameWithUserInfo(userInfo){
			if (userInfo.uname != undefined && userInfo.uname != null) {
				return userInfo.uname;
			}else if(userInfo.realname != undefined && userInfo.realname != null){
				return userInfo.realname;
			}else if(userInfo.rename != undefined && userInfo.rename != null){
				return userInfo.rename;
			}else{
				return '(未知用户)';
			}
		}
		function setupQA(){
			var userData = api.pageParam.user;
			var userName = '(未知用户)';
			var userIcon = '../../icon/icon_default_HeadPortrait.png';
			if (userData != null) {
				userName = userNameWithUserInfo(userData);
				userIcon = userData.picurl;
				if (userIcon == null || userIcon == '' || userIcon == 'null') {
					userIcon = '../../icon/icon_default_HeadPortrait.png';
				}
				var currentUid = userData.uid;
				if (currentUid == $api.getStorage('cfnetppuid')  || api.pageParam.userType == '1') {
					$('#delete_question').show();
				}
			}
			var subTime = timeWithStamp(api.pageParam.ctime.toString());
			$('#sub_que_user').text(userName);
			$('#que_detail').html(api.pageParam.comment);
			$('#sub_que_time').text(subTime);
			$('#user_icon').attr('src',userIcon);
		}
		function backClick(){
			api.closeWin();
		} 
		function onReplyClick(){
			api.openFrame({
			    name: 'Teacher_StudyCourseReply',
			    url: 'Teacher_StudyCourseReply.html',
			    rect: {
			        x: 0,
			        y: 66,
			        w: 'auto',
			        h: api.winHeight - 66
			    },
			    pageParam : api.pageParam
			});
		}
		function loadData(){
			var listParams = {
				jid : currentJid,
				fid : api.pageParam.id,
				page : page,
				rows : rows
			};
			// alert('url:'+url_replayQuestionList+'\n'+JSON.stringify(listParams));
			cfnetppPOST(url_replayQuestionList, listParams, true, function(data, status){
				api.refreshHeaderLoadDone();
				// alert(JSON.stringify(data));
				if (status == 'success') {
					if (data.status == true && data.code == 200) {
						if (page == 1) {
							questionDatas = new Array();
						}
						var questions = data.data;
						if (questions != null && questions.constructor == Array && questions.length > 0) {
							var currentIndex = questionDatas.length;
							refreshCount = questions.length;
							questionDatas = questionDatas.concat(questions);
							var questionListHtml = '';
							for (var i = currentIndex; i < questionDatas.length; i++) {
								var question = questionDatas[i];
								questionListHtml += questionHtmlWithData(question, i);
							}
							if (page == 1) {
								$('#questionList').html(questionListHtml);
							}else{
								$('#questionList').append(questionListHtml);
							}
							addCellClick();
							$('.noSign').hide();
			    			$('.loadFailure').hide();
						}else{
							if (page == 1) {
								$('.noSign').show();
				    			$('.loadFailure').hide();
							}
						}
					}else{
						if (page == 1) {
							$('.noSign').show();
			    			$('.loadFailure').hide();
						}
					}
				}else{
					if (page == 1) {
						$('.noSign').hide();
		    			$('.loadFailure').show();
					}
				}
			});
		}
		function loadNewData(){
			page = 1;
			loadData();
		}
		function loadMoreData(){
			page++;
			loadData();
		}
		function questionHtmlWithData(data, index) {
			// alert(JSON.stringify(data));
			var userInfo = data.user;
			var userName = '(未知用户)';
			var userIcon = '../../icon/icon_default_HeadPortrait.png';
			var removeHtml = '';
			if (userInfo != null) {
				userName = userNameWithUserInfo(userInfo);
				userIcon = userInfo.picurl;
				if (userIcon == null || userIcon == '' || userIcon == 'null') {
					userIcon = '../../icon/icon_default_HeadPortrait.png';
				}
				var currentUid = userInfo.uid;
				if (currentUid == $api.getStorage('cfnetppuid')  || api.pageParam.userType == '1') {
					removeHtml = '<span index="'+index+'" class="timeSpan rightSpan delete-reply">删除</span>';
				}
			}
			var timeStr = timeWithStamp(data.ctime.toString());
			return '<li id="'+data.id+'" index="'+index+'" class="QA_cell mui-table-view-cell">'+
				 	'<div class="answer-div">'+
						'<img class="touxiang" src="'+userIcon+'" alt="">'+
		    			'<label class="nameLabel">'+userName+'</label>'+
		    			'<div class="contentDiv">'+data.comment+'</div>'+
		    			'<span class="timeSpan">'+timeStr+'</span>'+
		    			removeHtml+
					'</div>'+
				 '</li>';
		}
		function addCellClick(){
			$('.QA_cell').unbind('click');
			$('.QA_cell').click(function(){
				var index = $(this).attr('index');
				var replyData = questionDatas[parseInt(index)];
			});
			$('.delete-reply').unbind('click');
			$('.delete-reply').click(function(event) {
				event.stopPropagation();
				var index = $(this).attr('index');
				var replyData = questionDatas[parseInt(index)];
				openDeleteReplyPage(replyData);
			});
		}
		function onDeleteQuestionClick(){
			var data = api.pageParam;
			data["deletecontent"] = "是否确认删除该问题？";
			data["deletetype"] = "questionDetail";
			api.openFrame({
			    name: 'deleteSomeData',
			    url: 'deleteSomeData.html',
			    pageParam: data,
			    bounces: false
			});
		}
		function openDeleteReplyPage(data){
			data["deletecontent"] = "是否确认删除该回复？";
			data["deletetype"] = "reply";
			api.openFrame({
			    name: 'deleteSomeData',
			    url: 'deleteSomeData.html',
			    pageParam: data,
			    bounces: false
			});
		}
		function deleteQuestion(questionData){
			var param = {
				faqid : questionData.id,
				jid : currentJid,
				uid : $api.getStorage('cfnetppuid'),
				pid : api.pageParam.planid,
				cid : api.pageParam.courseid
			}
			cfnetppPOST(url_deleteQuestionOrReplay, param, true, function(data, status){
			  	if(api.pageParam.userType == '1'){//老师身份
			      	if(questionData.removetype==0){//老师删除自己的提问
				      	param["message"]="提问";
					  	SendBusinessOperationsLogNotifi('答疑列表页', '018', param, param, '', data, '老师删除提问接口', '018-05')
				  	}else{
				      	param["message"]=questionData.user.rename+"的提问";//老师删除学生的提问
					  	SendBusinessOperationsLogNotifi('答疑列表页', '018', param, param, '', data, '老师删除学生提问接口', '018-07');
				 	}
			  	}else{
			  		param["message"]="问题"+"\""+notTen(questionData.comment)+"\"";
				  	SendBusinessOperationsLogNotifi('答疑列表页', '018', param, param, '', data, '学生删除提问接口', '018-02');
			  	}
				if (status == 'success') {
					if (data.code == 200) {
						api.toast({
						    msg: '删除成功'
						});
						api.sendEvent({
						    name: 'questionDetailDeleteNotifi',
						    extra: questionData
						});
						api.closeWin();
					}else{
						api.toast({
						    msg: '删除失败，请重试'
						});
					}
				}else{
					api.toast({
					    msg: '删除失败，请重试'
					});
				}
			});
		}
		function deleteReply(replyData){
			var param = {
				faqid : replyData.id,
				jid : currentJid,
				uid : $api.getStorage('cfnetppuid'),
				pid : api.pageParam.planid,
				cid : api.pageParam.courseid
			}
			cfnetppPOST(url_deleteReply, param, true, function(data, status){
                  
			  if(replyData.deletetype=="reply"){//删除回复
			     	if(api.pageParam.userType == '1'){//老师身份
				      	if(replyData.uid==$api.getStorage('cfnetppuid')){//提出问题人是老师自己
					    	 param["message"]="对"+api.pageParam.user.rename+"问题"+"回复";
					     	SendBusinessOperationsLogNotifi('答疑列表页', '018', param, param, '', data, '删除提问回复接口', '018-06')
					 	}else{//如果不是自己
					    	param["message"]=replyData.user.rename+"对"+api.pageParam.user.rename+"问题"+"回复";
					     	SendBusinessOperationsLogNotifi('答疑列表页', '018', param, param, '', data, '删除学生对某个学生提问回复接口', '018-08')
					  	}
//					  
				  	}else{
				  		param["message"]="对"+api.pageParam.user.rename+"问题"+"\""+notTen(replyData.comment)+"\""+"回复";
					 	 SendBusinessOperationsLogNotifi('答疑列表页', '018', param, param, '', data, '删除提问回复接口', '018-03');
				  	}
			    }else{//删除提问
			      	if(api.pageParam.userType == '1'){//老师身份
				      	param["message"]="提问";
					  	SendBusinessOperationsLogNotifi('答疑列表页', '018', param, param, '', data, '老师删除提问接口', '018-05')
				  	}else{
				  		param["message"]="问题"+"\""+notTen(replyData.comment)+"\"";
					  	SendBusinessOperationsLogNotifi('答疑列表页', '018', param, param, '', data, '学生删除提问接口', '018-02')
				  	}
			    }
				if (status == 'success') {
					if (data.code == 200) {
						$('#'+replyData.id).remove();
				    	if ($('#questionList').html() == '') {
				    		$('#noData').show();
				    	}
				    	api.sendEvent({
						    name: 'questionReplyDeleteNotifi',
						    extra: api.pageParam
						});
					}else{
						api.toast({
						    msg: '删除失败，请重试'
						});
					}
				}else{
					api.toast({
					    msg: '删除失败，请重试'
					});
				}
			});
		}
		//字符串截取10位
    function notTen(b) {
		var str = b;
		if (str.length > 13) {
			b = str.substr(0, 12) + '...';
			return b
		} else {
			return b
		}
    }
		//下拉刷新
		function setRefreshHeader() {
			api.setRefreshHeaderInfo({
				visible : true,
				bgColor : '#ccc',
				textColor : '#fff',
				textDown : '下拉刷新...',
				textUp : '松开刷新...',
				showTime : true
			}, function(ret, err) {
				loadNewData();
			});
		}
		// 上拉加载
		function setPullUpRefresh() {
			api.addEventListener({
				name : 'scrolltobottom'
			}, function(ret, err) {
				if (refreshCount >= rows) {
					loadMoreData();
				}
			});
		}
	</script>
</html>